package com.zeh.billing.controller;


import com.zeh.billing.config.BasePage;
import com.zeh.billing.config.CommonResult;
import com.zeh.billing.entity.*;
import com.zeh.billing.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zeh
 * @since 2021-08-20
 */
@RequestMapping("/customer")
@RestController
@CrossOrigin
@Api(tags = "客户 接口")
public class CustomerController {
@Autowired
private CustomerService customerService;

    @PostMapping("add")
    @ApiOperation("新增")
    public CommonResult register(@RequestBody @Validated CustomerAddDo customerdo){
        CommonResult add = customerService.add(customerdo);
        return  add;
    }


    @GetMapping("search/{cid}")
    @ApiOperation("查询用户")
    public CommonResult search(@PathVariable String cid){
        CommonResult search = customerService.search(cid);
        return search;
    }

    @GetMapping("searchAll")
    @ApiOperation("查询全部用户")
    public CommonResult searchAll(BasePage basePage){
        CommonResult search = customerService.searchAll(basePage);
        return search;
    }

    @DeleteMapping("delete/{Cid}")
    @ApiOperation("删除用户")
    public CommonResult delete(@PathVariable String Cid){
        Integer delete = customerService.delete(Cid);
        if(delete == 1){
            return CommonResult.success("删除成功！");
        }else{
            return CommonResult.failed("删除失败！");
        }
    }

    @PostMapping("money")
    @ApiOperation("金额减少")
    public CommonResult chargeMoney(@RequestBody  CustomerChargeDto chargeDto){
        boolean b = customerService.chargeMoney(chargeDto);
        if(b){
            return CommonResult.success("更改余额成功！");

        }else{
            return CommonResult.failed("金额不足,消费失败！");
        }
    }

    @PostMapping("addMoney")
    @ApiOperation("金额增加")
    public CommonResult addMoney(@RequestBody addMoneyDto addDto){
        boolean b = customerService.addMoney(addDto);
        if(b){
            return CommonResult.success("更改余额成功！");

        }else{
            return CommonResult.failed("更改失败！");
        }
    }

    @PostMapping("addCard")
    @ApiOperation("增加会员卡")
    public CommonResult addCard(@RequestBody addCardDo cardDo){
        boolean b = customerService.addCard(cardDo);
        if(b){
            return CommonResult.success("更改余额成功！");

        }else{
            return CommonResult.failed("更改失败！");
        }
    }

    @PostMapping("searchCustomer")
    @ApiOperation("模糊查询人员")
    public CommonResult searchCustomer(@RequestBody SearchCustomerDo customerDo){
        List<CustomerVo> customerVo =  customerService.searchCustomer(customerDo);
     return CommonResult.success("查询成功！",customerVo);
    }







}

